/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.enterprise.model.PurchaseOrganization;
import org.crystalrange.enterprise.service.PurchaseOrganizationService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class PurchaseOrganizationListController extends BaseController {

    protected PurchaseOrganizationService purchaseOrganizationService;
    protected PurchaseOrganization selectedPurchaseOrganization;
    protected Listbox listboxPurchaseOrganization;
    protected Search searchPurchaseOrganization;
    protected List<PurchaseOrganization> purchaseOrganizations;

    public PurchaseOrganizationService getPurchaseOrganizationService() {
        return this.purchaseOrganizationService;
    }

    public void setPurchaseOrganizationService(PurchaseOrganizationService purchaseOrganizationService) {
        this.purchaseOrganizationService = purchaseOrganizationService;
    }

    public PurchaseOrganization getSelectedPurchaseOrganization() {
        return this.selectedPurchaseOrganization;
    }

    public void setSelectedPurchaseOrganization(PurchaseOrganization selectedPurchaseOrganization) {
        this.selectedPurchaseOrganization = selectedPurchaseOrganization;
    }

    public List<PurchaseOrganization> getPurchaseOrganizations() {
        return this.purchaseOrganizations;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchPurchaseOrganization = new Search(PurchaseOrganization.class);
        this.searchPurchaseOrganization.setMaxResults(100);
        this.searchPurchaseOrganization.addFetch("createdBy");
        this.searchPurchaseOrganization.addFetch("updatedBy");
        this.purchaseOrganizations = this.purchaseOrganizationService.search(this.searchPurchaseOrganization);

        component.setAttribute("controller", this);
    }

    public void onClick$menuitemPurchaseOrganizationCreate(Event event) throws Exception {
        this.purchaseOrganizationCreate();
    }

    public void onClick$menuitemPurchaseOrganizationEdit(Event event) throws Exception {
        this.purchaseOrganizationEdit();
    }

    public void onClick$menuitemPurchaseOrganizationDelete(Event event) throws Exception {
        this.purchaseOrganizationDelete();
    }

    public void onClick$menuitemPurchaseOrganizationSearch(Event event) throws Exception {
        this.purchaseOrganizationSearch();
    }

    public void onClick$menuitemPurchaseOrganizationRefresh(Event event) throws Exception {
        this.purchaseOrganizationRefresh();
    }

    public void onClick$toolbarbuttonPurchaseOrganizationCreate(Event event) throws Exception {
        this.purchaseOrganizationCreate();
    }

    public void onClick$toolbarbuttonPurchaseOrganizationEdit(Event event) throws Exception {
        this.purchaseOrganizationEdit();
    }

    public void onClick$toolbarbuttonPurchaseOrganizationDelete(Event event) throws Exception {
        this.purchaseOrganizationDelete();
    }

    public void onClick$toolbarbuttonPurchaseOrganizationSearch(Event event) throws Exception {
        this.purchaseOrganizationSearch();
    }

    public void onClick$toolbarbuttonPurchaseOrganizationRefresh(Event event) throws Exception {
        this.purchaseOrganizationRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.purchaseOrganizationEdit();
    }
    
    private void purchaseOrganizationCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/purchaseOrganization.zul");
    }

    private void purchaseOrganizationEdit() throws Exception {
        if (this.selectedPurchaseOrganization != null) {
            this.execution.sendRedirect("~./zul/html/purchaseOrganization.zul?Id=" + this.selectedPurchaseOrganization.getId().toString());
        }
    }

    private void purchaseOrganizationDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxPurchaseOrganization.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    PurchaseOrganization purchaseOrganization = (PurchaseOrganization) selectedItem.getValue();
                    this.purchaseOrganizationService.remove(purchaseOrganization);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.purchaseOrganizations = this.purchaseOrganizationService.search(this.searchPurchaseOrganization);
        this.binder.loadAll();
    }

    private void purchaseOrganizationSearch() throws Exception {
        this.execution.setAttribute("searchPurchaseOrganization", this.searchPurchaseOrganization);

        Window window = (Window) this.execution.createComponents("~./zul/html/purchaseOrganizationSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchPurchaseOrganization = (Search) this.execution.getAttribute("searchPurchaseOrganization");
        }

        this.purchaseOrganizations = this.purchaseOrganizationService.search(this.searchPurchaseOrganization);
        this.binder.loadAll();
    }

    private void purchaseOrganizationRefresh() throws Exception {
        this.purchaseOrganizations = this.purchaseOrganizationService.search(this.searchPurchaseOrganization);
        this.binder.loadAll();
    }
}
